package com.jlf.controller.engine;

import com.jlf.controller.engine.entity.index.SchemaIndexDetail;
import com.jlf.controller.engine.entity.index.SchemaIndexInput;
import com.jlf.core.annotation.restful.controller.RestfulController;
import com.jlf.core.annotation.restful.method.RestfulDelete;
import com.jlf.core.annotation.restful.method.RestfulGet;
import com.jlf.core.annotation.restful.method.RestfulPost;
import com.jlf.core.annotation.restful.method.RestfulPut;
import com.jlf.core.engine.domain.SchemaDomainDO;
import com.jlf.core.engine.domain.SchemaFieldDO;
import com.jlf.core.engine.domain.SchemaIndexDO;
import com.jlf.core.engine.entity.index.EditIndexWrapper;
import com.jlf.core.engine.persistence.SchemaDomainPersistence;
import com.jlf.core.engine.persistence.SchemaFieldPersistence;
import com.jlf.core.engine.persistence.SchemaIndexPersistence;
import com.jlf.core.engine.query.SchemaIndexQueryInput;
import com.jlf.core.engine.server.SchemaIndexServer;
import com.jlf.core.error.exception.NoObjectException;
import com.jlf.core.error.exception.NullReferenceException;
import com.jlf.core.output.ListResultWrapper;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * 模型的索引接口控制器
 *
 * @author wujr
 * 2023/7/16
 * @version 1.0
 * 【变更历史】
 * [wujr 2023/7/16 1.0 新增]
 */
@Slf4j
@Tag(name = "模型领域索引管理接口")
@RestfulController(path = "/rest/jlf/core/engine/schema/indexes", name = "模型领域索引管理接口", domainClass = SchemaIndexDO.class)
@RequiredArgsConstructor
public class SchemaIndexController {
    private final SchemaIndexServer schemaIndexServer;
    private final SchemaDomainPersistence schemaDomainPersistence;
    private final SchemaFieldPersistence schemaFieldPersistence;
    private final SchemaIndexPersistence schemaIndexPersistence;

    @RestfulPost(code = "addSchemaIndex", name = "新增模型领域索引", path = "/domain/{domainId}")
    public SchemaIndexDO addSchemaIndex(@Valid @RequestBody SchemaIndexInput input,
                                        @PathVariable("domainId") Long domainId) throws Exception{
        SchemaDomainDO schemaDomain = schemaDomainPersistence.getById(domainId);
        if (schemaDomain == null){
            log.info("新增模型领域索引，所属模型：{} 不存在", domainId);
            throw new NullReferenceException(SchemaIndexDO.class, "模型不存在");
        }
        List<SchemaFieldDO> schemaFields = schemaFieldPersistence.loadByDomain(schemaDomain.getDomainCode(), null);
        SchemaIndexDO record = input.to();
        record.setDomainCode(schemaDomain.getDomainCode());
        return schemaIndexServer.add(record, schemaFields);
    }

    @RestfulPut(code = "editSchemaIndex", name = "编辑模型领域索引", path = "/{indexId}")
    public SchemaIndexDO editSchemaIndex(@PathVariable("indexId") Long indexId,
                                         @Valid @RequestBody SchemaIndexInput input) throws Exception {
        SchemaIndexDO original = schemaIndexPersistence.getById(indexId);
        if (original == null){
            log.info("编辑模型领域索引：{}，索引不存在", indexId);
            throw new NoObjectException(SchemaIndexDO.class, "模型领域索引");
        }
        SchemaIndexDO record = input.to();
        record.setDomainCode(original.getDomainCode());
        EditIndexWrapper wrapper = new EditIndexWrapper();
        wrapper.setOriginal(original);
        wrapper.setRecord(record);
        List<SchemaFieldDO> schemaFields = schemaFieldPersistence.loadByDomain(original.getDomainCode(), null);
        wrapper.setSchemaFields(schemaFields);
        schemaIndexServer.edit(wrapper);
        return wrapper.getRecord();
    }

    @RestfulDelete(code = "deleteSchemaIndex", name = "删除模型领域索引", path = "/{indexId}")
    public void deleteSchemaIndex(@PathVariable("indexId") Long indexId) throws Exception{
        SchemaIndexDO record = schemaIndexPersistence.getById(indexId);
        if (record == null){
            log.info("删除模型领域索引：{}，索引不存在", indexId);
            throw new NoObjectException(SchemaIndexDO.class, "模型领域索引");
        }
        schemaIndexServer.delete(record);
    }

    @RestfulGet(code = "querySchemaIndex", name = "查询模型领域索引")
    public ListResultWrapper<SchemaIndexDO> querySchemaIndex(SchemaIndexQueryInput input,
                                                             @RequestParam(value = "pageNo", required = false, defaultValue = "1") int pageNo,
                                                             @RequestParam(value = "pageSize", required = false, defaultValue = "50") int pageSize){
        return schemaIndexServer.query(input, pageNo, pageSize);
    }

    @RestfulGet(code = "viewSchemaIndex", name = "查看模型领域索引详情", path = "/{indexId}")
    public SchemaIndexDetail viewSchemaIndex(@PathVariable("indexId") Long indexId) throws Exception{
        SchemaIndexDO record = schemaIndexPersistence.getById(indexId);
        if (record == null){
            log.info("查看模型领域索引详情：{}，索引不存在", indexId);
            throw new NoObjectException(SchemaIndexDO.class, "模型领域索引");
        }
        return new SchemaIndexDetail(record);
    }
}
